package gui_layer;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ScrollPaneConstants;
import javax.swing.event.InternalFrameEvent;

import model_layer.Supplier;
import ctr_layer.SupplierCtr;

public class NewSupplierGUI extends JInternalFrame {
	
	private SupplierCtr supplctr;
	private Supplier suppl;
	private boolean edit;
	private JTextField name_tf;
	private JTextField phone_tf;
	private JTextField email_tf;
	private JTextField address_tf;
	private JTextField city_tf;
	private JTextField zip_tf;
	private JTextField cvr_tf;
	private JTextArea pref_txt;
	private JScrollPane scroll_txt;
	private JLabel lblName;
	private JLabel lblPhone;
	private JLabel lblEmail;
	private JLabel lblAddress;
	private JLabel lblCity;
	private JLabel lblZipCode;
	private JLabel lblCvr;
	private JLabel lblDesc;
	private JLabel lblincname;
	private JLabel lblinvname;
	private JLabel lblincphone;
	private JLabel lblinvemail;
	private JLabel lblincemail; 
	private JLabel lblincadd;
	private JLabel lblinccity;
	private JLabel lblinczip;
	private JLabel lblinccvr;
	private JLabel inc_name_taken_lbl;
	private JButton btnCreate;
	private JButton btnCancel;
	private JButton btnEdit;
	private JButton btnOk;
	private JLabel[] empty_lbls;
	
	/**
	 * Create the frame.
	 */
	public NewSupplierGUI(Supplier newSuppl, boolean edit) {
		supplctr=new SupplierCtr();
		this.edit=edit;
		setClosable(true);
		setIconifiable(true);
		setResizable(false);
		setDefaultCloseOperation(JInternalFrame.DISPOSE_ON_CLOSE);
		try
		{
			setFrameIcon(new ImageIcon(NewAccountGUI.class.getResource("/img/icon.png")));
		}
		catch (Exception e)
		{	
		}
		setBounds(100, 100, 345, 380);
		getContentPane().setLayout(null);
		getContentPane().setBackground(Color.getHSBColor(312, 0.11f, 0.17f));
		suppl=newSuppl;
		make_tfs();
		make_lbls();
		if (suppl == null)
		{
			setTitle("Create Supplier");
			btn_create();
			btn_cancel();
			make_inc_lbls();
			add_listeners();
		}
		else if (edit)
		{
			setTitle("Edit Supplier");
			btn_edit();
			btn_cancel();
			make_inc_lbls();
			fill_fields();
			add_listeners();
		}
		else
		{
			setTitle("Supplier Details");
			btn_ok();
			fill_fields();
			set_uneditable();
		}
		
		
	}
	
	private void btn_create()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		btnCreate = new JButton("Create");
		btnCreate.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCreate.setForeground(Color.WHITE);
		btnCreate.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCreate.setContentAreaFilled(false);
		btnCreate.setOpaque(true);
		btnCreate.setBorder(new RoundedCornerBorder());
		btnCreate.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				create_suppl();
			}
		});
		btnCreate.setBounds(10, 316, 89, 23); 
		getContentPane().add(btnCreate);
	}
	private void btn_cancel()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		btnCancel = new JButton("Cancel");
		btnCancel.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnCancel.setForeground(Color.WHITE);
		btnCancel.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnCancel.setContentAreaFilled(false);
		btnCancel.setOpaque(true);
		btnCancel.setBorder(new RoundedCornerBorder());
		btnCancel.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
			}
		});
		btnCancel.setBounds(230, 316, 89, 23);
		getContentPane().add(btnCancel);
	}
	
	private void btn_edit()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		btnEdit = new JButton("Save");
		btnEdit.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnEdit.setForeground(Color.WHITE);
		btnEdit.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnEdit.setContentAreaFilled(false);
		btnEdit.setOpaque(true);
		btnEdit.setBorder(new RoundedCornerBorder());
		btnEdit.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				edit_suppl();
			}
		});
		btnEdit.setBounds(10, 316, 89, 23); 
		getContentPane().add(btnEdit);
	}
	
	private void btn_ok()
	{
		float[] values = Color.RGBtoHSB(255, 104, 0, null);
		btnOk = new JButton("OK");
		btnOk.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		btnOk.setForeground(Color.WHITE);
		btnOk.setBackground(Color.getHSBColor(values[0], values[1], values[2]));
		btnOk.setContentAreaFilled(false);
		btnOk.setOpaque(true);
		btnOk.setBorder(new RoundedCornerBorder());
		btnOk.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg0)
			{
				dispose();
			}
		});
		btnOk.setBounds(120, 316, 89, 23);
		getContentPane().add(btnOk);
	}
	
	private void make_tfs()
	{
		name_tf = new JTextField();
		name_tf.setBounds(180, 8, 137, 20);
		name_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		name_tf.setBorder(new RoundedCornerBorder());
		getContentPane().add(name_tf);
		name_tf.setColumns(10);
		
		phone_tf = new JTextField();
		phone_tf.setBounds(180, 33, 137, 20);
		phone_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		phone_tf.setBorder(new RoundedCornerBorder());
		getContentPane().add(phone_tf);
		phone_tf.setColumns(10);
		
		email_tf = new JTextField();
		email_tf.setBounds(180, 58, 137, 20);
		email_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		email_tf.setBorder(new RoundedCornerBorder());
		getContentPane().add(email_tf);
		email_tf.setColumns(10);
		
		address_tf = new JTextField();
		address_tf.setBounds(180, 83, 137, 20);
		address_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		address_tf.setBorder(new RoundedCornerBorder());
		getContentPane().add(address_tf);
		address_tf.setColumns(10);
		
		city_tf = new JTextField();
		city_tf.setBounds(180, 133, 137, 20);
		city_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		city_tf.setBorder(new RoundedCornerBorder());
		getContentPane().add(city_tf);
		city_tf.setColumns(10);
		
		zip_tf = new JTextField();
		zip_tf.setBounds(180, 108, 137, 20);
		zip_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		zip_tf.setBorder(new RoundedCornerBorder());
		getContentPane().add(zip_tf);
		zip_tf.setColumns(10);
		
		cvr_tf = new JTextField();
		cvr_tf.setBounds(180, 158, 137, 20);
		cvr_tf.setBorder(new RoundedCornerBorder());
		cvr_tf.setBorder(new RoundedCornerBorder());
		cvr_tf.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		getContentPane().add(cvr_tf);
		cvr_tf.setColumns(10);
		
		pref_txt = new JTextArea();
		pref_txt.setLineWrap(true);			//used for line wrapping - feature of continuing on a new line when a line is full instead of getting a horizontal scroll bar.
		pref_txt.setWrapStyleWord(true); 	//used for not splitting one word on different lines.
		pref_txt.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		
		scroll_txt = new JScrollPane(pref_txt);
		scroll_txt.setLocation(10, 209);
		scroll_txt.setSize(309, 96);
		scroll_txt.setBorder(new RoundedCornerBorder());
		scroll_txt.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		scroll_txt.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		getContentPane().add(scroll_txt);
	}
	
	private void make_lbls()
	{
		
		lblName = new JLabel("Name");
		lblName.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblName.setForeground(Color.WHITE);
		lblName.setBounds(10, 11, 46, 14);
		getContentPane().add(lblName);
		
		lblPhone = new JLabel("Phone Number");
		lblPhone.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblPhone.setForeground(Color.WHITE);
		lblPhone.setBounds(10, 36, 103, 14);
		getContentPane().add(lblPhone);
		
		lblEmail = new JLabel("E-mail");
		lblEmail.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblEmail.setForeground(Color.WHITE);
		lblEmail.setBounds(10, 61, 46, 14);
		getContentPane().add(lblEmail);
		
		lblAddress = new JLabel("Address");
		lblAddress.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblAddress.setForeground(Color.WHITE);
		lblAddress.setBounds(10, 86, 75, 14);
		getContentPane().add(lblAddress);
		
		lblCity = new JLabel("City");
		lblCity.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblCity.setForeground(Color.WHITE);
		lblCity.setBounds(10, 136, 46, 14);
		getContentPane().add(lblCity);
		
		lblZipCode = new JLabel("Zip Code");
		lblZipCode.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblZipCode.setForeground(Color.WHITE);
		lblZipCode.setBounds(10, 111, 75, 14);
		getContentPane().add(lblZipCode);
		
		lblCvr = new JLabel("CVR-number");
		lblCvr.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblCvr.setForeground(Color.WHITE);
		lblCvr.setBounds(10, 161, 75, 14);
		getContentPane().add(lblCvr);
		
		lblDesc = new JLabel("Description:");
		lblDesc.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblDesc.setForeground(Color.WHITE);
		lblDesc.setBounds(10, 187, 75, 14);
		getContentPane().add(lblDesc);
		
	}
	
	private void make_inc_lbls()
	{

		lblincname = new JLabel("Must be less than 64 characters");
		lblincname.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblincname.setBounds(327, 11, 176, 14);
		lblincname.setForeground(Color.RED);
		getContentPane().add(lblincname);
		
		lblinvname = new JLabel("Name cannot start with digit");
		lblinvname.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblinvname.setBounds(327, 11, 176, 14);
		lblinvname.setForeground(Color.RED);
		getContentPane().add(lblinvname);
		
		lblincphone = new JLabel("Invalid Phone Number");
		lblincphone.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblincphone.setBounds(327, 36, 154, 14);
		lblincphone.setForeground(Color.RED);
		getContentPane().add(lblincphone);
		
		lblinvemail = new JLabel("Invalid E-mail ");
		lblinvemail.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblinvemail.setBounds(327, 61, 89, 14);
		lblinvemail.setForeground(Color.RED);
		getContentPane().add(lblinvemail);
		
		lblincemail = new JLabel("Must be less than 256 characters");
		lblincemail.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblincemail.setBounds(327, 61, 191, 14);
		lblincemail.setForeground(Color.RED);
		getContentPane().add(lblincemail);
		
		lblincadd = new JLabel("Must be less than 64 characters");
		lblincadd.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblincadd.setBounds(327, 86, 191, 17);
		lblincadd.setForeground(Color.RED);
		getContentPane().add(lblincadd);
		
		lblinczip = new JLabel("Invalid Zip Code");
		lblinczip.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblinczip.setBounds(327, 111, 154, 14);
		lblinczip.setForeground(Color.RED);
		getContentPane().add(lblinczip);
		
		lblinccity = new JLabel("Must be less than 64 characters");
		lblinccity.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblinccity.setBounds(327, 136, 191, 14);
		lblinccity.setForeground(Color.RED);
		getContentPane().add(lblinccity);
		
		lblinccvr = new JLabel("Invalid CVR ");
		lblinccvr.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		lblinccvr.setBounds(327, 161, 154, 14);
		lblinccvr.setForeground(Color.RED);
		getContentPane().add(lblinccvr);
		
		inc_name_taken_lbl = new JLabel("Name already exists.");
		inc_name_taken_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
		inc_name_taken_lbl.setForeground(Color.RED);
		inc_name_taken_lbl.setBounds(327, 11, 114, 14);
		getContentPane().add(inc_name_taken_lbl);
		
		
		empty_lbls = new JLabel[7];
		for(int i = 0; i < 7; i++)
		{
			JLabel empty_lbl = new JLabel("Field cannot be empty");
			empty_lbl.setFont(new Font("Helvetica Neue", Font.BOLD, 11));
			empty_lbl.setBounds(327, 11 + 25*i, 140, 14);
			empty_lbl.setForeground(Color.RED);
			getContentPane().add(empty_lbl);
			empty_lbls[i] = empty_lbl;
		}
		
		set_inc_lbl_invisible();
	}
	
	private void fill_fields()
	{
		name_tf.setText(suppl.getName());
		phone_tf.setText(suppl.getPhone_nr());
		email_tf.setText(suppl.getEmail());
		address_tf.setText(suppl.getAddress());
		zip_tf.setText(suppl.getZipcode());
		city_tf.setText(suppl.getCity());
		cvr_tf.setText(suppl.getCvr());
		pref_txt.setText(suppl.getDescription());
	}
	
	private void set_uneditable()
	{
		name_tf.setEditable(false);
		phone_tf.setEditable(false);
		email_tf.setEditable(false);
		city_tf.setEditable(false);
		address_tf.setEditable(false);
		zip_tf.setEditable(false);
		cvr_tf.setEditable(false);
		pref_txt.setEditable(false);
	}
	private void set_inc_lbl_invisible()
	{
		lblincname.setVisible(false);
		lblinvname.setVisible(false);
		lblincphone.setVisible(false);
		lblinvemail.setVisible(false);
		lblincemail.setVisible(false);
		lblincadd.setVisible(false);
		lblinccity.setVisible(false);
		lblinczip.setVisible(false);
		lblinccvr.setVisible(false);
		inc_name_taken_lbl.setVisible(false);
		
		for(int i = 0; i < 7; i++)
		{
			empty_lbls[i].setVisible(false);
		}
	}
	
	private boolean check_for_mistakes()
	{
		set_inc_lbl_invisible();
		boolean has_mistake = false;
		boolean is_email_ok = true;
		boolean is_phone_ok = true;
		String name = name_tf.getText();
		String phone = phone_tf.getText();
		String email = email_tf.getText();
		String address = address_tf.getText();
		String zip = zip_tf.getText();
		String city = city_tf.getText();
		String cvr = cvr_tf.getText();
		
		if (name.length()==0)
		{
			empty_lbls[0].setVisible(true);
			has_mistake = true;
		}
		else if (name.length()>64)
		{
			lblincname.setVisible(true);
			has_mistake=true;
		}
		else if (Character.isDigit(name.charAt(0)))
		{
			lblinvname.setVisible(true);
			has_mistake=true;
		}
		else if(suppl == null && supplctr.name_exists(name) || (suppl != null && !suppl.getName().equals(name) && supplctr.name_exists(name)))
		{
			inc_name_taken_lbl.setVisible(true);
			has_mistake = true;
		}
			
		if ((phone.length() != 0) && ((!phone.matches("(?:\\+[0-9]{2,2}\\-?)?[0-9](?:\\-?[0-9]){7,10}")) || (phone.length() > 16)))
		{
			lblincphone.setVisible(true);
			has_mistake = true;
			is_phone_ok = false;
		}
		
		if (email.length()>256)
		{
			lblincemail.setVisible(true);
			has_mistake = true;
			is_email_ok = false;
		}
		else if ((email.length()!=0)&&(!email.matches("^[\\w\\._%\\+\\-]{1,64}@[\\w\\.\\-]{1,188}\\.[A-Za-z]{2,4}$")))
		{
			lblinvemail.setVisible(true);
			has_mistake = true;
			is_email_ok = false;
		}
		
		if ((phone.length()==0)||(email.length()==0))
		{
			if ((phone.length()==0)&&(email.length()==0))
			{
				empty_lbls[1].setVisible(true);
				empty_lbls[2].setVisible(true);
				has_mistake = true;
			}
			else if ((phone.length() == 0) && (is_email_ok == false))
			{
				empty_lbls[1].setVisible(true);
				has_mistake = true;
			}
			else if (is_phone_ok == false)
			{
				empty_lbls[2].setVisible(true);
				has_mistake = true;
			}
		}
		if (address.length()==0)
		{
			empty_lbls[3].setVisible(true);
			has_mistake = true;
		}
		else if (address.length()>64)
		{
			lblincadd.setVisible(true);
			has_mistake = true;
		}
		
		if (city.length()==0)
		{
			empty_lbls[5].setVisible(true);
			has_mistake = true;
		}
		else if (city.length()>32)
		{
			lblinccity.setVisible(true);
			has_mistake = true;
		}
		
		if (zip.length()==0)
		{
			empty_lbls[4].setVisible(true);
			has_mistake = true;
		}
		else if (convert_zipcode(zip)==null)
		{
			lblinczip.setVisible(true);
			has_mistake = true;
		}
		
		if (cvr.length()==0)
		{
			empty_lbls[6].setVisible(true);
			has_mistake = true;
		}
		else if ((cvr.length()>16)||(!cvr.matches("[0-9A-Z]{6,16}")))
		{
			lblinccvr.setVisible(true);
			has_mistake = true;	
		}
		
		if(has_mistake)
		{
			change_to_display_incorrect();
		}
		else
		{
			change_to_display_correct();
		}
		
		return has_mistake;
	}
	
	private void create_suppl()
	{
		if(!check_for_mistakes())
		{
			String name = name_tf.getText();
			String phone = phone_tf.getText();
			String email = email_tf.getText();
			String address = address_tf.getText();
			String city = city_tf.getText();
			String zipcode = zip_tf.getText();
			String cvr = cvr_tf.getText();
			String description = pref_txt.getText();
			if(JOptionPane.showConfirmDialog(this, "Are you sure you want to create a supplier?", "Create supplier", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(supplctr.create_supplier(name, phone, email, address, city, convert_zipcode(zipcode), cvr, description))
				{
					JOptionPane.showMessageDialog(this, "Supplier was successfully created!", "Success", JOptionPane.PLAIN_MESSAGE);
					dispose();
					fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Error occured while creating a supplier!\nSupplier was not inserted.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	private void edit_suppl()
	{
		if (!check_for_mistakes())
		{
			
			String name = name_tf.getText();
			String phone = phone_tf.getText();
			String email = email_tf.getText();
			String address = address_tf.getText();
			String city = city_tf.getText();
			String zipcode = zip_tf.getText();
			String cvr = cvr_tf.getText();
			String description = pref_txt.getText();
			if(JOptionPane.showConfirmDialog(this, "Are you sure you want to update this supplier?", "Edit supplier", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				suppl.setName(name);
				suppl.setPhone_nr(phone);
				suppl.setEmail(email);
				suppl.setAddress(address);
				suppl.setCity(city);
				suppl.setZipcode(convert_zipcode(zipcode));
				suppl.setCvr(cvr);
				suppl.setDescription(description);
				if(supplctr.update_supplier(suppl))
				{
					JOptionPane.showMessageDialog(this, "Supplier was successfully updated!", "Success", JOptionPane.PLAIN_MESSAGE);
					dispose();
					fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSING);
				}
				else
				{
					JOptionPane.showMessageDialog(this, "Error occured while saving a supplier!\nSupplier was not updated.", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}
	
	private String convert_zipcode(String zipcode)
	{
		if(zipcode.matches("^[0-9]{4,4}$"))
		{
			return "BG" + zipcode;
		}
		else if(zipcode.matches("^[A-Z]{2,3}(?:\\-?[0-9]){4,7}"))
		{
			return zipcode.replace("-", "");
		}
		else return null;
	}
	
	private boolean is_int(String input)
	{
		try
		{
			Integer.parseInt(input);
			return true;
		}
		catch(NumberFormatException e)
		{
			return false;
		}
	}
	
	private void change_to_display_correct()
	{
		setSize(345, 380);
		btnCancel.setBounds(230, 316, 89, 23);
	}
	
	private void change_to_display_incorrect()
	{
		setSize(544, 380);
		btnCancel.setBounds(429, 316, 89, 23);
	}
	
	private void add_listeners()
	{
		name_tf.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					phone_tf.requestFocus();
				}
			}
		});
		
		phone_tf.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					email_tf.requestFocus();
				}
			}
		});
		
		email_tf.addKeyListener(new KeyAdapter()
		{
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					address_tf.requestFocus();
				}
			}
		});
		
		address_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					zip_tf.requestFocus();
				}
			}
		});
		city_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					cvr_tf.requestFocus();
				}
			}
		});
		
		zip_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					city_tf.requestFocus();
				}
			}
		});

		zip_tf.addFocusListener(new FocusAdapter()
		{
			@Override
			public void focusLost(FocusEvent e)
			{
				String zipcode = convert_zipcode(zip_tf.getText());
				if(zipcode != null)
				{
					if((suppl == null) || (!suppl.getZipcode().equals(zipcode)))
					{
						String city = supplctr.find_city_by_zipcode(zipcode);
						if(city != null)
						{
							city_tf.setText(city);
						}
					}
				}
			}
		});
		
		cvr_tf.addKeyListener(new KeyAdapter()
		{
			@Override
			public void keyPressed(KeyEvent e)
			{
				if(e.getKeyCode() == KeyEvent.VK_ENTER)
				{
					pref_txt.requestFocus();
				}
			}
		});
		pref_txt.addKeyListener(new KeyAdapter()
		{
		   public void keyPressed(KeyEvent e) {
		    	if(e.getKeyCode() == KeyEvent.VK_TAB) {
		    		System.out.println(e.getModifiers());
		    		if (suppl == null)
		    		{
		    			pref_txt.transferFocus();
		    		}
		    		else if (edit)
		    		{
		    			pref_txt.transferFocus();
		    		}
		    		else
		    		{
		    			pref_txt.transferFocus();
		    		}	
		    		e.consume();
		    	}
		    }
		});
	}
}
